package vn.egame.elockscreen.receive;

import vn.egame.elockscreen.ELockscreenPreferences;
import vn.egame.elockscreen.activity.LockScreenAnimationActivity;
import vn.egame.elockscreen.activity.LockScreenDefaultActivity;
import vn.egame.elockscreen.service.ScreenLockerService;
import vn.egame.elockscreen.utils.Constans;
import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class LockerReceive extends BroadcastReceiver {
	private KeyguardManager mKeyguardManager;
	private KeyguardLock mKeyguardLock;

	public static boolean isScreenOn;

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		ELockscreenPreferences.getInstance().initPreferences(context);
		if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
			// Toast.makeText(context, "screen is off", Toast.LENGTH_SHORT)
			// .show();
			isScreenOn = false;
			System.out.println("screen is off");

			if (ELockscreenPreferences.getInstance().isEnableLockscreen()
//					&& !ScreenLockerService.isShowLock
					&& !ScreenLockerService.isCall) {

				if (ELockscreenPreferences.getInstance()
						.getLockerPackageTemplate().equals("0")) {
					Intent i = new Intent(context,
							LockScreenDefaultActivity.class)
							.putExtra(
									Constans.START_SCREEN_SAVER_TYPE,
									Constans.StartScreenSaverType.SCREEN_OFF
											.getValue()).addFlags(
									Intent.FLAG_ACTIVITY_NEW_TASK);

					context.startActivity(i);
				} else {
					Intent i = new Intent(context,
							LockScreenAnimationActivity.class)
							.putExtra(
									Constans.START_SCREEN_SAVER_TYPE,
									Constans.StartScreenSaverType.SCREEN_OFF
											.getValue()).addFlags(
									Intent.FLAG_ACTIVITY_NEW_TASK);

					context.startActivity(i);
				}

			}
		} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
			isScreenOn = true;

			if (ELockscreenPreferences.getInstance().isEnableLockscreen()) {
				mKeyguardManager = (KeyguardManager) context
						.getSystemService(Context.KEYGUARD_SERVICE);
				mKeyguardLock = mKeyguardManager.newKeyguardLock("FxLock");

				mKeyguardLock.disableKeyguard();

			}

			System.out.println("screen is on");

		} else if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
			if (ELockscreenPreferences.getInstance().isEnableLockscreen()) {
			
				mKeyguardManager = (KeyguardManager) context
						.getSystemService(Context.KEYGUARD_SERVICE);
				mKeyguardLock = mKeyguardManager.newKeyguardLock("FxLock");

				mKeyguardLock.disableKeyguard();

				if (ELockscreenPreferences.getInstance()
						.getLockerPackageTemplate().equals("0")) {
					Intent i = new Intent(context,
							LockScreenDefaultActivity.class)
							.putExtra(
									Constans.START_SCREEN_SAVER_TYPE,
									Constans.StartScreenSaverType.SCREEN_OFF
											.getValue()).addFlags(
									Intent.FLAG_ACTIVITY_NEW_TASK);

					context.startActivity(i);
				} else {
					Intent i = new Intent(context,
							LockScreenAnimationActivity.class)
							.putExtra(
									Constans.START_SCREEN_SAVER_TYPE,
									Constans.StartScreenSaverType.SCREEN_OFF
											.getValue()).addFlags(
									Intent.FLAG_ACTIVITY_NEW_TASK);

					context.startActivity(i);
				}
			}

			// context.startService(new Intent(context, Servicelock.class));
			// Intent i = new Intent(context, LockScreenActivity.class);
			// i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			// context.startActivity(i);

//			Log.d("Kai", "ACTION_BOOT_COMPLETED");

		}
	}

}
